import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/HomePage.vue')
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/About.vue')
  },
  {
    path: '/userInfo',
    name: 'userInfo',
    component: () => import('../views/MyUser.vue')
  },
  {
    path: '/',
    redirect: '/home'
  },
]

const limitRouterArr = [
  '/userInfo',
]

const router = new VueRouter({
  routes
})

router.beforeEach((to ,from ,next) => {
  console.log('beforeEach',to.path, from.path);

  if (!limitRouterArr.includes(to.path)) {
    console.log('没拦截');
    return next()
  }
 
  console.log('拦截了');
  next('/about')

})

// 解决路由守卫连续触发的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

export default router
